package com.worldwizards.smut.serialization;

import java.io.IOException;
import java.io.ObjectStreamConstants;
import java.util.ArrayList;
import java.util.List;

public class SerializedClassRecord extends ParseObject {
	String _name;
	long _suid;
	private byte _classDescFlags;
	private FieldDesc[] _fields;
	private List<ParseObject> _annotations = new ArrayList<ParseObject>();
	private SerializedClassRecord _superClass;
	
	
	public SerializedClassRecord(SerializedObjectRoot root, String name, int handle,
			long serialVersionUID, BinaryParser parser) throws IOException, SerializationParsingException {
		super(handle);
		_name = name;
		_suid = serialVersionUID;
		_classDescFlags = parser.readByte();
		short fieldCount = parser.readShort();
		_fields = new FieldDesc[fieldCount];
		for(int i=0;i<fieldCount;i++){
			_fields[i] = new FieldDesc(root,this,parser);
		}
		while (parser.peekByte()!=ObjectStreamConstants.TC_ENDBLOCKDATA){
			_annotations.add(root.parseContent(parser));
		}
		parser.readByte(); // throw away TC_ENDBLOCKDATA
		_superClass=root.parseClassDesc(parser);
	}

	public boolean isExternalizable() {
		// TODO Auto-generated method stub
		return false;
	}
	public boolean hasWriteMethod() {
		// TODO Auto-generated method stub
		return false;
	}

	
	public List<ValueRecord> parseValues(SerializedObjectRoot root, BinaryParser parser) throws IOException, SerializationParsingException {
		List<ValueRecord> vlist = new ArrayList<ValueRecord>();
		parseValues(root,vlist,parser);
		return vlist;
	}

	protected void parseValues(SerializedObjectRoot root, List<ValueRecord> vlist, BinaryParser parser) throws IOException, SerializationParsingException {
		for (FieldDesc field : _fields){
			vlist.add(field.parse(root,parser));
		}
		if (_superClass != null){
			_superClass.parseValues(root,vlist,parser);
		}
	}
	@Override
	protected void dumpDetails(String prefix) {
		System.out.println(prefix+"Class: "+_name);
		System.out.println(prefix+"   SUID: "+_suid);
		System.out.println(prefix+"   Flags: "+Integer.toHexString(0xFF&_classDescFlags));
		for (FieldDesc field : _fields){
			field.dump(prefix+"   ");
		}
		for (ParseObject annotation: _annotations){
			annotation.dump(prefix+"   ");
		}
		if (_superClass!=null){
			System.out.print(prefix+"   Super class:");
			_superClass.dump(prefix+"   ");
		}
	}

	public String getClassName() {
		return _name;
	}

}
